<?php

namespace Gairal\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Gairal\CoreBundle\Form\MotivationType;
use \Gairal\CoreBundle\Entity\Motivation;

class MotivationAdminController extends Controller {

    public function modifyAction() {
        
         $motivation = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Motivation')
                ->findIt();
         
         //First time when DB is empty
         if($motivation == null){
             $motivation = new Motivation ();
         }

        $form = $this->createForm(new MotivationType, $motivation);

        // On récupère la requête
        $request = $this->get('request');
        // On vérifie qu'elle est de type POST
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
            // On vérifie que les valeurs entrées sont correctes
            // (Nous verrons la validation des objets en détail dans le prochain chapitre)
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager(); 
                $em->persist($motivation);
                $em->flush();
                
                $this->get('session')->getFlashBag()->add('info', 'Motivation is up to date, it\'s a perfect description of yourself and your goals. And you\'re the best');
                return $this->redirect($this->generateUrl('AdminMotivation', array('id' => $motivation->getId())));
            }
        }

        return $this->render('GairalAdminBundle:Motivation:modify.html.twig', array(
                    'action' => 'Admin - Motivation - Modify',
                    'form' => $form->createView()
        ));
    }

}